﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace PortScan
{
    class Scanner
    {
        private Server server;
        private int Start;
        private int End;
        


        public Scanner(Server server)
        {
            this.server = server;

        }

        public void CheckRangeNormal(int port_start, int port_end)
        {
            for (int portnum = port_start; portnum <= port_end; portnum++)
            {

            }
        }
    }

    class PortCheck
    {
        public static PortInfo Check(IPAddress ip, int port)
        {
            PortInfo pi = new PortInfo(ip, port, false);//closed until proven to be opened
            try
            {
                TcpClient client = new TcpClient();
                client.Connect(ip, port);
                client.Close(); //Close se we can test other ports without raising an error
                pi.open = true; //Port is open
                return pi;
            }
            catch (Exception e)
            {
                //Port is closed or an error occured like wrong ip or connection problem 
                //Improvement possible is to analyse the error returned
                //Console.WriteLine(e);
                return pi;
            }
        }

        public static void test()
        {
            Console.WriteLine("do some work");
        }
    }

    class Server //holds server network information
    {
        public string hostname;
        public List<IPAddress> ipv4addrs;
        public List<IPAddress> ipv6addrs;
        public IPAddress[] addrs;

        public Server(string host)
        {
            this.hostname = host;
            try
            {
                this.addrs = Dns.GetHostEntry(hostname).AddressList;
                this.ipv4addrs = new List<IPAddress>();
                this.ipv6addrs = new List<IPAddress>();
                this.sortIpAddresses();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public void sortIpAddresses()
        {
            foreach (IPAddress ip in addrs)
            {
                if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                {
                    this.ipv6addrs.Add(ip);
                }
                else
                {
                    this.ipv4addrs.Add(ip);
                }
            }
        }
    }
    class PortInfo //holds port information ip number and wether it is open or not
    {
        public IPAddress ipaddr;
        public int number;
        public bool open;

        public PortInfo(IPAddress ipaddr, int number, bool open)
        {
            this.ipaddr = ipaddr;
            this.number = number;
            this.open = open;
        }
    }
}
